﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace Student.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ExamController : ControllerBase
    {

        // GET api/<ExamController>
        [HttpGet]
        public string Get()
        {
            List<int> students = GetStudents();
            List<int> evens = GetEven(students);
            List<int> odds = GetOdd(students);
            List<int> results = new List<int>();
            for (int i = 0; i < odds.Count; i++)
            {
                results.Add(odds[i]);
                if (i < evens.Count)
                {
                    results.Add(evens[i]);
                }
            }
            //log.Info(string.Format("本次排序后考生：", string.Join(",", results)));
            return string.Join(",",results);
        }


        private List<int> GetStudents()
        {
            List<int> results = new List<int>();
            Random random = new Random();
            int count= random.Next(20,100);
            for (int i = 1; i <= count; i++)
            {
                results.Add(i);
            }
            //log.Info(string.Format("本次生成最初考生：", string.Join(",", results)));
            return results;
        }

        private List<int> GetEven(List<int> ints)
        {
            List<int> result = new List<int>();
            foreach (int i in ints)
            {
                if (i % 2 == 0)
                {
                    result.Add(i);
                }
            }
            result.Sort((a, b) => b.CompareTo(a));
            return result;
        }

        private List<int> GetOdd(List<int> ints)
        {
            List<int> result = new List<int>();
            foreach (int i in ints)
            {
                if (i % 2 == 1)
                {
                    result.Add(i);
                }
            }
            return result;
        }
    }
}
